<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="继承，派生，超类">
<meta property="og:type" content="article">
<meta property="og:title" content="Java日拱一卒-day5">
<meta property="og:url" content="http://yoursite.com/2020/03/28/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day5/index.html">
<meta property="og:site_name" content="很惭愧，就做了一点微小的工作">
<meta property="og:description" content="继承，派生，超类">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-03-28T15:19:18.000Z">
<meta property="article:modified_time" content="2020-07-01T13:56:11.301Z">
<meta property="article:author" content="文予">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/03/28/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day5/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Java日拱一卒-day5 | 很惭愧，就做了一点微小的工作</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">很惭愧，就做了一点微小的工作</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">念念不忘，必有回响</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/28/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/wenyu.png">
      <meta itemprop="name" content="文予">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="很惭愧，就做了一点微小的工作">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java日拱一卒-day5
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-03-28 23:19:18" itemprop="dateCreated datePublished" datetime="2020-03-28T23:19:18+08:00">2020-03-28</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-07-01 21:56:11" itemprop="dateModified" datetime="2020-07-01T21:56:11+08:00">2020-07-01</time>
              </span>

          
            <div class="post-description">继承，派生，超类</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="Object所有类的超类"><a href="#Object所有类的超类" class="headerlink" title="Object所有类的超类"></a><code>Object</code>所有类的超类</h2><p><code>Object</code>类是Java所有类的始祖，Java中每个类都是有由它扩展而来（即<code>extend</code>，继承）。如果没有明确指出超类，<code>Object</code>就被认为是这个类的超类。可以使用<code>Object</code>类型的变量引用任何类型的对象，反过来，所有的Java对象都可以调用<code>java.lang.Object</code>类所定义的方法。从子类角度来看，子类扩展（<code>extend</code>）了父类，从父类来看，父类派生（<code>derive</code>）出了字类。换言之，继承与派生所描述的是同一个动作，只是观察角度不同。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestObject</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//User的父亲是Object</span></span><br><span class="line">        User user = <span class="keyword">new</span> User();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取user对象父类</span></span><br><span class="line">        System.out.println( user.getClass() );      <span class="comment">//class cn.tedu.hello.day05.pojo.User 全局限定名</span></span><br><span class="line">        System.out.println( user.getClass().getSuperclass() );              <span class="comment">//super 父类，超类 class java.lang.Object</span></span><br><span class="line"></span><br><span class="line">        System.out.println();</span><br><span class="line"></span><br><span class="line">        Integer i = <span class="number">100</span>;        <span class="comment">//包装类型</span></span><br><span class="line">        System.out.println( i.getClass().getName() );</span><br><span class="line">        System.out.println( i.getClass().getSuperclass() );</span><br><span class="line">        System.out.println( i.getClass().getSuperclass().getSuperclass() );</span><br><span class="line">        System.out.println( i.getClass().getSuperclass().getSuperclass().getSuperclass() );</span><br><span class="line"></span><br><span class="line">        System.out.println();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//所有类的根都是Object，所以java中万物皆对象</span></span><br><span class="line">        Double PI = <span class="number">3.14</span>;</span><br><span class="line">        System.out.println( PI.getClass().getName() );</span><br><span class="line">        System.out.println( PI.getClass().getSuperclass() );</span><br><span class="line">        System.out.println( PI.getClass().getSuperclass().getSuperclass() );</span><br><span class="line"></span><br><span class="line">        System.out.println();</span><br><span class="line"></span><br><span class="line">        Character c = <span class="string">'A'</span>;</span><br><span class="line">        System.out.println( c.getClass().getName() );</span><br><span class="line">        System.out.println( c.getClass().getSuperclass() );</span><br><span class="line">        System.out.println( c.getClass().getSuperclass().getSuperclass() );</span><br><span class="line"></span><br><span class="line">        String s = <span class="string">"abc"</span>;</span><br><span class="line">        System.out.println( s.getClass().getSuperclass() );</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="常见的方法"><a href="#常见的方法" class="headerlink" title="常见的方法"></a>常见的方法</h2><h3 id="format方法"><a href="#format方法" class="headerlink" title="format方法"></a><code>format</code>方法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">format</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//format 格式化</span></span><br><span class="line">        String s1 = <span class="string">"abc"</span>;</span><br><span class="line">        System.out.println(<span class="string">"format 格式化值："</span> + s1);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//%s占位符，占这个位置，通过后面参数替换，s1替换%s</span></span><br><span class="line">        System.out.println( s1.format(<span class="string">"format 格式化值：%s"</span>, s1) );</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="concat方法"><a href="#concat方法" class="headerlink" title="concat方法"></a><code>concat</code>方法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">concat</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String s1 = <span class="string">"abc"</span>;</span><br><span class="line">        String s2 = <span class="string">"def"</span>;</span><br><span class="line">        System.out.println( s1+s2 );            <span class="comment">//本质就是concat，字符串连接符简写</span></span><br><span class="line"></span><br><span class="line">        System.out.println( s1.concat(s2) );</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="length方法"><a href="#length方法" class="headerlink" title="length方法"></a><code>length</code>方法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">length</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String s = <span class="string">"汲黯，字长孺，濮阳人也"</span>;</span><br><span class="line">        System.out.println( s.length() );</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="charAt获取某个位置的字符"><a href="#charAt获取某个位置的字符" class="headerlink" title="charAt获取某个位置的字符"></a><code>charAt</code>获取某个位置的字符</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">charAt</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String s = <span class="string">"汲黯字长孺濮阳人也其先有宠于古之卫君"</span>;</span><br><span class="line">        System.out.println(<span class="string">"第一个字符是："</span> + s.charAt(<span class="number">0</span>) );</span><br><span class="line"></span><br><span class="line">        <span class="comment">//java.lang.StringIndexOutOfBoundsException: String index out of range: 3</span></span><br><span class="line">        <span class="comment">//常见错误，StringIndex字符串索引位置，OutOfBounds 超过范围，越界</span></span><br><span class="line">        System.out.println(<span class="string">"最后一个字符是："</span> + s.charAt(<span class="number">18</span>-<span class="number">1</span>) );</span><br><span class="line"></span><br><span class="line">        <span class="comment">//写代码一定灵活，通用</span></span><br><span class="line">        System.out.println(<span class="string">"最后一个字符是："</span> + s.charAt( s.length()-<span class="number">1</span> ));</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="subtring截取字符串"><a href="#subtring截取字符串" class="headerlink" title="subtring截取字符串"></a><code>subtring</code>截取字符串</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">substring</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    String s = <span class="string">"汲黯字长孺濮阳人也"</span>;</span><br><span class="line">    <span class="comment">//要求：截取“汲黯”</span></span><br><span class="line">    String s1 = s.substring(<span class="number">0</span>, <span class="number">2</span>);</span><br><span class="line">    System.out.println( s1 );</span><br><span class="line"></span><br><span class="line">    <span class="comment">//要求：截取“长孺”</span></span><br><span class="line">    String s2 = s.substring(<span class="number">3</span>, <span class="number">5</span>);</span><br><span class="line">    System.out.println( s2 );</span><br><span class="line"></span><br><span class="line">    <span class="comment">//要求：截取“濮阳人也”</span></span><br><span class="line">    String s3 = s.substring(<span class="number">5</span>, s.length());</span><br><span class="line">    System.out.println(s3);</span><br><span class="line">    String s4 = s.substring(<span class="number">5</span>);     <span class="comment">//默认截到字符串最后</span></span><br><span class="line">    System.out.println(s4);</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>

<h3 id="身份证号码解析"><a href="#身份证号码解析" class="headerlink" title="身份证号码解析"></a>身份证号码解析</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestCardNo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//利用java提供基础api实现身份证解析</span></span><br><span class="line">        <span class="comment">//六位数字地址码，八位数字出生日期码，三位数字顺序码和一位校验码。</span></span><br><span class="line"></span><br><span class="line">        String cardNo = <span class="string">"61011319850314212X"</span>;       <span class="comment">//18位</span></span><br><span class="line">        String address = cardNo.substring(<span class="number">0</span>, <span class="number">6</span>);        <span class="comment">//地址码，截串</span></span><br><span class="line"></span><br><span class="line">        String birthday = cardNo.substring(<span class="number">6</span>,<span class="number">14</span>);       <span class="comment">//生日，截串，1985年3月4日</span></span><br><span class="line">        <span class="keyword">int</span> year = Integer.parseInt( birthday.substring(<span class="number">0</span>,<span class="number">4</span>) ); <span class="comment">//Integer.parseInt()把字符串转成int整数</span></span><br><span class="line">        <span class="keyword">int</span> month = Integer.parseInt( birthday.substring(<span class="number">4</span>,<span class="number">6</span>) );        <span class="comment">//通过类型转换把0去掉</span></span><br><span class="line">        <span class="keyword">int</span> day = Integer.parseInt( birthday.substring(<span class="number">6</span>,<span class="number">8</span>) );</span><br><span class="line"></span><br><span class="line">        String serno = cardNo.substring( cardNo.length()-<span class="number">4</span>, cardNo.length()-<span class="number">1</span> );            <span class="comment">//顺序号</span></span><br><span class="line">        <span class="keyword">int</span> gender = Integer.parseInt(cardNo.substring(<span class="number">16</span>,<span class="number">17</span>));     <span class="comment">//性别</span></span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"身份证号："</span>+ cardNo);</span><br><span class="line">        System.out.println(<span class="string">"地址码："</span>+ address);</span><br><span class="line">        System.out.println(<span class="string">"出生年月："</span>+ year+<span class="string">"年"</span>+month+<span class="string">"月"</span>+day+<span class="string">"日"</span>);</span><br><span class="line">        System.out.println(<span class="string">"序列号："</span>+ serno);</span><br><span class="line">        <span class="keyword">if</span>( gender%<span class="number">2</span> ==<span class="number">0</span>) &#123;</span><br><span class="line">            System.out.println(<span class="string">"性别：女"</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">"性别：男"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="用星号替代字符串中的数字"><a href="#用星号替代字符串中的数字" class="headerlink" title="用星号替代字符串中的数字"></a>用星号替代字符串中的数字</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestStar</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//需求把，数字变成*</span></span><br><span class="line">        String s = <span class="string">"王家辉他的收入3456万元"</span>;</span><br><span class="line">        <span class="comment">//思考：提取每一个字符,使用循环来一个个判断,如果是数字就替换*号其他正常输出</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//for循环</span></span><br><span class="line">        String r = <span class="string">""</span>;      <span class="comment">//空串</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i&lt; s.length(); i++) &#123;</span><br><span class="line">            <span class="comment">//System.out.println( s.charAt(i) );</span></span><br><span class="line">            <span class="comment">//如何判断是数字呢？ char值，字母ASCII，数字0(48)~9(57) ascii</span></span><br><span class="line">            <span class="keyword">if</span>( s.charAt(i) &gt;=<span class="number">48</span> &amp;&amp; s.charAt(i)&lt;=<span class="number">57</span>) &#123;</span><br><span class="line">                r += <span class="string">"*"</span>;               <span class="comment">//r = r + "*"; 前面语句缩写</span></span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                r += s.charAt(i);       <span class="comment">//r = r + s.charAt(i);</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(r);</span><br><span class="line"></span><br><span class="line">        System.out.println();</span><br><span class="line">        r = <span class="string">""</span>; <span class="comment">//清空</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//使用foreach循环？变成数组，然后获取数组的每个值，判断如果数字就替换</span></span><br><span class="line">        <span class="keyword">char</span>[] cs = s.toCharArray();            <span class="comment">//获取字符串所有字符，放到字符数组中</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//foreach循环语法结构：  数组元素值 临时变量名称（每一次循环取出一个值） : 数组集合</span></span><br><span class="line">        <span class="comment">// for ( char y : cs)</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">char</span> a : cs) &#123;  <span class="comment">//语法，格式</span></span><br><span class="line">            <span class="keyword">if</span>( a &gt;= <span class="number">48</span> &amp;&amp; a&lt;=<span class="number">57</span>) &#123;</span><br><span class="line">                r += <span class="string">"*"</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                r += a;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(r);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/03/28/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day4/" rel="prev" title="Java日拱一卒--day4">
      <i class="fa fa-chevron-left"></i> Java日拱一卒--day4
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/03/30/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day6/" rel="next" title="Java日拱一卒-day6">
      Java日拱一卒-day6 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Object所有类的超类"><span class="nav-number">1.</span> <span class="nav-text">Object所有类的超类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#常见的方法"><span class="nav-number">2.</span> <span class="nav-text">常见的方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#format方法"><span class="nav-number">2.1.</span> <span class="nav-text">format方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#concat方法"><span class="nav-number">2.2.</span> <span class="nav-text">concat方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#length方法"><span class="nav-number">2.3.</span> <span class="nav-text">length方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#charAt获取某个位置的字符"><span class="nav-number">2.4.</span> <span class="nav-text">charAt获取某个位置的字符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#subtring截取字符串"><span class="nav-number">2.5.</span> <span class="nav-text">subtring截取字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#身份证号码解析"><span class="nav-number">2.6.</span> <span class="nav-text">身份证号码解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#用星号替代字符串中的数字"><span class="nav-number">2.7.</span> <span class="nav-text">用星号替代字符串中的数字</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="文予"
      src="/uploads/wenyu.png">
  <p class="site-author-name" itemprop="name">文予</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文予</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
